package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import beans.Bookmark;
import beans.User;

import model.BookmarkDAO;
import model.Model;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */

public class ManageAction extends Action{
	
	private BookmarkDAO bookmarkDAO;
	
	public ManageAction(Model model){
		
		bookmarkDAO = model.getBookmarkDAO();
	}
	
	@Override
	public String getName() {
		return "manage.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors",errors);
		HttpSession session = request.getSession();
		
		if(session.getAttribute("aerrors") != null){
			request.setAttribute("aerrors", (List<String>)request.getSession().getAttribute("aerrors"));
			session.setAttribute("aerrors", null);
		}
		
		if(session.getAttribute("derrors") != null){
			errors.addAll( (List<String>)request.getSession().getAttribute("derrors"));
			session.setAttribute("derrors", null);
		}
		
        try{
			User user = (User)request.getSession().getAttribute("user");			
				
			if(user == null){
					
				return "login.jsp";
			}else{
				List<Bookmark> bookmarks = bookmarkDAO.getUserBookmarks(user.getUserEmail());
				
				request.setAttribute("managingBookmarks", bookmarks);
			}
			
			request.setAttribute("page", "m");			
			return "manage.jsp";
			
		}catch(DAOException e){
			errors.add(e.toString());
			return "manage.jsp";
		}
	}
}
